package com.mt.takeway.shiro;

import com.mt.takeway.po.Customer;
import com.mt.takeway.service.CustomerService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/***
 *@title myRealm
 *@author chl
 *@create 2022/5/26 21:27
 */
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private CustomerService customerService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }


    //认证处理
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        String principal = (String)authenticationToken.getPrincipal();
        Customer customer = customerService.findCustomerByTel(principal,"123456");
        String password = customer.getPassword();
        String realmName = getName();

        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(principal, password, realmName);

        return authenticationInfo;
    }
}
